package com.jsgt.greekfire.ui.mine.user.address.addcontactperson

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.scan.android.CaptureActivity
import com.fhx.core.utils.ToastUtils
import com.ftd.livepermissions.LivePermissions
import com.ftd.livepermissions.PermissionResult
import com.jsgt.greekfire.BuildConfig
import com.jsgt.greekfire.R
import com.jsgt.greekfire.SCAN_RESULT
import com.jsgt.greekfire.dao.model.ContactAddress
import com.jsgt.greekfire.dao.viewmodel.AddressViewModel
import kotlinx.android.synthetic.main.activity_add_contact_person.*

/**
 * 添加联系人
 * author fhx
 * create time 2020/4/23 14:04
 **/
class AddContactPersonActivity : BaseActivity() {
    private val addressViewModel: AddressViewModel by lazy {
        ViewModelProvider(this).get(AddressViewModel::class.java)
    }

    private var launch: ActivityResultLauncher<Intent>? = null
    private var contactAddress: ContactAddress? = null

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int = R.layout.activity_add_contact_person

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        contactAddress = intent.getParcelableExtra("address")
        if (contactAddress == null) {
            title = getString(R.string.address_create)
        } else {
            title = getString(R.string.address_edit)
            txvDelete.visibility = View.VISIBLE
            txvDelete.setOnClickListener {
                showProgress(true)
                addressViewModel.delete(contactAddress!!.id)
                txvDelete.postDelayed({
                    finish()
                }, 1000)
            }
            edtAddress.setText(contactAddress!!.address)
            edtName.setText(contactAddress!!.name)
            edtDesc.setText(contactAddress!!.description)
        }
        setRightText(getString(R.string.save)).setOnClickListener {
            addContact()
        }

        imvScan.setOnClickListener {
            LivePermissions(this).request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA
            ).observe(this, Observer {
                when (it) {
                    //扫一扫
                    is PermissionResult.Grant -> {
                        launch?.launch(Intent(this, CaptureActivity::class.java))
                    }
                    is PermissionResult.Rationale -> {

                    }
                    is PermissionResult.Deny -> {
                        ToastUtils.show(R.string.permission_denied)
                    }
                }
            })
        }

        launch = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Activity.RESULT_OK) {
                val content = it.data?.getStringExtra(SCAN_RESULT)
                edtAddress.setText(content)
                edtAddress.setSelection(edtAddress.text.length)
            }
        }
    }

    private fun addContact() {
        val name = edtName.text.toString().trim()
        val desc = edtDesc.text.toString().trim()
        val address = edtAddress.text.toString().trim()
        if (TextUtils.isEmpty(address)) {
            ToastUtils.show(R.string.address_address_not_null)
            return
        }
        if (TextUtils.isEmpty(name)) {
            ToastUtils.show(R.string.address_name_not_null)
            return
        }
        if (!address.startsWith(BuildConfig.WALLET_PRIVATE_KEY_START) || address.length != 36) {
            ToastUtils.show(R.string.address_address_error)
            return
        }
        showProgress(true)

        val contactAddress = ContactAddress()
        contactAddress.address = address
        contactAddress.key = "ATR"
        contactAddress.name = name
        contactAddress.description = desc
        addressViewModel.add(contactAddress)

        edtAddress.postDelayed({
            finish()
        }, 1000)
    }
}